﻿using CTM.Web.Models;
using CTM.Web.Models.Input;
using CTM.Web.Utils;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;

namespace CTM.Web
{
    public class EmailService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            // Plug in your email service here to send an email.
            return Task.FromResult(0);
        }
    }

    public class SmsService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            // Plug in your SMS service here to send a text message.
            return Task.FromResult(0);
        }
    }

    // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
        {
        }

        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };

            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };

            // Configure user lockout defaults
            manager.UserLockoutEnabledByDefault = true;
            manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
            manager.MaxFailedAccessAttemptsBeforeLockout = 5;

            // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
            // You can write your own provider and plug it in here.
            manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
            {
                MessageFormat = "Your security code is {0}"
            });
            manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
            {
                Subject = "Security Code",
                BodyFormat = "Your security code is {0}"
            });
            manager.EmailService = new EmailService();
            manager.SmsService = new SmsService();
            var dataProtectionProvider = options.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider =
                    new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
            return manager;
        }
    }

    // Configure the application sign-in manager which is used in this application.
    public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
    {
        public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
            : base(userManager, authenticationManager)
        {
        }

        public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
        {
            return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
        }

        public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
        {
            return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
        }
    }

    // Configure the application role manager used in this application. RoleManager is defined in ASP.NET Identity and is used by the application.
    public class ApplicationRoleManager : RoleManager<ApplicationRole>
    {
        public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store)
            : base(store)
        {
        }

        public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
        {
            var manager = new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>()));
            //manager.RoleValidator = new RoleValidator<ApplicationRole>(manager){};
            return manager;
        }
    }

    public class ApplicationInitializer :
        //DropCreateDatabaseAlways<ApplicationDbContext>
    DropCreateDatabaseIfModelChanges<ApplicationDbContext>
    {
        protected override void Seed(ApplicationDbContext context)
        {
            InitializeIdentityForEF(context);
            base.Seed(context);
        }

        private void InitializeIdentityForEF(ApplicationDbContext context)
        {
            ApplicationRoleManager roleManager = new ApplicationRoleManager(new RoleStore<ApplicationRole>(context));
            ApplicationUserManager userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));

            AddAdministrator(context, roleManager, userManager);
            AddHR(context, roleManager, userManager);

            AddBUHead(context, roleManager, userManager);
            AddSectionHead(context, roleManager, userManager);
            AddDepartmentHead(context, roleManager, userManager);
            AddGroupManager(context, roleManager, userManager);
            AddTeamMember(context, roleManager, userManager);

            AddOrganization(userManager, context);

            AddCategories(userManager, context);
            AddConversation1(userManager, context);
            AddConversation2(userManager, context);
            AddConversation3(userManager, context);
            AddConversation4(userManager, context);
            AddConversation5(userManager, context);
            AddConversation6(userManager, context);

            AddFeedback1(userManager, context);
        }

        private void AddCategories(ApplicationUserManager userManager, ApplicationDbContext context)
        {
            var category1 = new Category { Name = "Conversation", IsDeleted = false, CreatedOn = DateTime.UtcNow };
            var category2 = new Category { Name = "Feedback", IsDeleted = false, CreatedOn = DateTime.UtcNow };
            var category3 = new Category { Name = "Note", IsDeleted = false, CreatedOn = DateTime.UtcNow };
            context.Categories.Add(category1);
            context.Categories.Add(category2);
            context.Categories.Add(category3);

            context.SaveChanges();
        }

        private void AddConversation1(ApplicationUserManager userManager, ApplicationDbContext context)
        {
            var conversationCategory = context.Categories.SingleOrDefault(c => c.Name == "Conversation");
            if (conversationCategory == null) return;

            const string managerName = "Anil.Kadakol@in.bosch.com";
            var manager = userManager.FindByName(managerName);

            const string employeeName = "Benno.Oommen@in.bosch.com";
            var employee = userManager.FindByName(employeeName);

            var conversation1 = new Post
            {
                Id = Guid.NewGuid(),
                Title = "Setting Employee Goals Effectively is Critical to Your Success",
                Description = "Employees who clearly understand their individual goals—and how they relate to the larger goals of the company—naturally become more engaged with their work. Successful companies realize that their achievements are linked to their ability to manage, track and communicate goals – informing their workforce and linking reward systems with individual and team performance to keep them engaged in their work.",
                Emoticon = Utils.Emoticon.Smile,
                IsDeleted = false,
                CreatedOn = new DateTime(2015, 8, 14, 9, 30, 20),
                CreatedBy = manager.Id,
                Type = Utils.PostType.Goals,
                Manager = manager,
                Employee = employee,
                Category = conversationCategory
            };

            // add comments
            var comment1 = new Comment
            {
                Id = Guid.NewGuid(),
                Content = "President Lyndon B. Johnson signs the Civil Rights Act of 1964, mandating equal opportunity employment, and complete desegregation of schools and other public facilities. It also outlaws unequal voter registration requirements.",
                IsDeleted = false,
                CreatedOn = new DateTime(2015, 8, 14, 12, 30, 20),
                CreatedBy = manager.Id,
            };
            conversation1.Comments.Add(comment1);

            var comment2 = new Comment
            {
                Id = Guid.NewGuid(),
                Content = "President Lyndon B. Johnson signs the Civil Rights Act of 1964, mandating equal opportunity employment, and complete desegregation of schools and other public facilities. It also outlaws unequal voter registration requirements.",
                IsDeleted = false,
                CreatedOn = new DateTime(2015, 8, 15, 9, 30, 20),
                CreatedBy = employee.Id,
            };
            conversation1.Comments.Add(comment2);

            // add tags
            var tag1 = context.Tags.SingleOrDefault(t => t.Name == "project");
            if (tag1 == null) tag1 = new Tag { Name = "project" };
            conversation1.Tags.Add(tag1);

            var tag2 = context.Tags.SingleOrDefault(t => t.Name == "career-path");
            if (tag2 == null) tag2 = new Tag { Name = "career-path" };
            conversation1.Tags.Add(tag2);

            conversation1 = context.Posts.Add(conversation1);
            context.SaveChanges();
        }

        private void AddConversation2(ApplicationUserManager userManager, ApplicationDbContext context)
        {
            var conversationCategory = context.Categories.SingleOrDefault(c => c.Name == "Conversation");
            if (conversationCategory == null) return;

            const string managerName = "Sriram.AswathNarayan@in.bosch.com";
            var manager = userManager.FindByName(managerName);

            const string employeeName = "Benno.Oommen@in.bosch.com";
            var employee = userManager.FindByName(employeeName);

            // conversation 2
            var conversation2 = new Post
            {
                Id = Guid.NewGuid(),
                Title = "Open communication about career goals and professional development",
                Description = "Review your personal career plan. Determine where you want to be in three, five and 10 years and what you need to do to get there. Consider if this path is possible at your current company, or whether it has limited opportunity to move horizontally into a new area of work. List the opportunities for learning and advancing within your company so you can focus a talk with your manager on those issues.",
                Emoticon = Utils.Emoticon.Neutral,
                IsDeleted = false,
                CreatedOn = new DateTime(2015, 9, 4, 9, 30, 20),
                CreatedBy = manager.Id,
                Type = Utils.PostType.Individual,
                Manager = manager,
                Employee = employee,
                Category = conversationCategory
            };

            var comment3 = new Comment
            {
                Id = Guid.NewGuid(),
                Content = "President Lyndon B. Johnson signs the Civil Rights Act of 1964, mandating equal opportunity employment, and complete desegregation of schools and other public facilities. It also outlaws unequal voter registration requirements.",
                IsDeleted = false,
                CreatedOn = new DateTime(2015, 9, 4, 11, 30, 20),
                CreatedBy = manager.Id,
            };
            conversation2.Comments.Add(comment3);

            var tag3 = context.Tags.SingleOrDefault(t => t.Name == "career-path");
            if (tag3 == null) tag3 = new Tag { Name = "career-path" };
            conversation2.Tags.Add(tag3);

            conversation2 = context.Posts.Add(conversation2);
            context.SaveChanges();
        }

        private void AddConversation3(ApplicationUserManager userManager, ApplicationDbContext context)
        {
            var conversationCategory = context.Categories.SingleOrDefault(c => c.Name == "Conversation");
            if (conversationCategory == null) return;

            const string managerName = "Rincy.Joseph@in.bosch.com";
            var manager = userManager.FindByName(managerName);

            const string employeeName = "Benno.Oommen@in.bosch.com";
            var employee = userManager.FindByName(employeeName);

            // conversation 3
            var conversation3 = new Post
            {
                Id = Guid.NewGuid(),
                Title = "The top five \"very important\" aspects of job satisfaction",
                Description = "In a survey by the Society for Human Resource Management in 2007, the top five \"very important\" aspects of job satisfaction, according to employees themselves, were:" +
                Environment.NewLine + "1. Compensation" +
                Environment.NewLine + "2. Benefits" +
                Environment.NewLine + "3. Job security" +
                Environment.NewLine + "4. Work/life balance, and" +
                Environment.NewLine + "5. Communication between employees and senior management.",
                Emoticon = Utils.Emoticon.Frown,
                IsDeleted = false,
                CreatedOn = new DateTime(2014, 9, 4, 9, 30, 20),
                CreatedBy = manager.Id,
                Type = Utils.PostType.Individual,
                Manager = manager,
                Employee = employee,
                Category = conversationCategory
            };

            var comment4 = new Comment
            {
                Id = Guid.NewGuid(),
                Content = "President Lyndon B. Johnson signs the Civil Rights Act of 1964, mandating equal opportunity employment, and complete desegregation of schools and other public facilities. It also outlaws unequal voter registration requirements.",
                IsDeleted = false,
                CreatedOn = new DateTime(2014, 9, 4, 11, 30, 20),
                CreatedBy = manager.Id,
            };
            conversation3.Comments.Add(comment4);

            var tag4 = context.Tags.SingleOrDefault(t => t.Name == "individual");
            if (tag4 == null) tag4 = new Tag { Name = "individual" };
            conversation3.Tags.Add(tag4);

            conversation3 = context.Posts.Add(conversation3);
            context.SaveChanges();
        }

        private void AddConversation4(ApplicationUserManager userManager, ApplicationDbContext context)
        {
            var conversationCategory = context.Categories.SingleOrDefault(c => c.Name == "Conversation");
            if (conversationCategory == null) return;

            const string managerName = "Anil.Kadakol@in.bosch.com";
            var manager = userManager.FindByName(managerName);

            const string employeeName = "Benno.Oommen@in.bosch.com";
            var employee = userManager.FindByName(employeeName);

            // conversation 4
            var conversation4 = new Post
            {
                Id = Guid.NewGuid(),
                Title = "Dealing With Anger",
                Description = "When you meet with someone who is angry, you can use the tools of effective listening to help defuse this anger. Nevertheless, when anger is directed at you, it is much more difficult to respond definitively, because your own emotions are usually involved.",
                Emoticon = Utils.Emoticon.Smile,
                IsDeleted = false,
                CreatedOn = new DateTime(2014, 5, 24, 15, 32, 20),
                CreatedBy = manager.Id,
                Type = Utils.PostType.Individual,
                Manager = manager,
                Employee = employee,
                Category = conversationCategory
            };

            var comment5 = new Comment
            {
                Id = Guid.NewGuid(),
                Content = "President Lyndon B. Johnson signs the Civil Rights Act of 1964, mandating equal opportunity employment, and complete desegregation of schools and other public facilities. It also outlaws unequal voter registration requirements.",
                IsDeleted = false,
                CreatedOn = new DateTime(2014, 6, 1, 11, 30, 20),
                CreatedBy = manager.Id,
            };
            conversation4.Comments.Add(comment5);

            var tag4 = context.Tags.SingleOrDefault(t => t.Name == "personal");
            if (tag4 == null) tag4 = new Tag { Name = "personal" };
            conversation4.Tags.Add(tag4);

            conversation4 = context.Posts.Add(conversation4);
            context.SaveChanges();
        }

        private void AddConversation5(ApplicationUserManager userManager, ApplicationDbContext context)
        {
            var conversationCategory = context.Categories.SingleOrDefault(c => c.Name == "Conversation");
            if (conversationCategory == null) return;

            const string managerName = "quynh.nguyennhat@vn.bosch.com";
            var manager = userManager.FindByName(managerName);

            const string employeeName = "Benno.Oommen@in.bosch.com";
            var employee = userManager.FindByName(employeeName);

            // conversation 5
            var conversation5 = new Post
            {
                Id = Guid.NewGuid(),
                Title = "Conversation Title 5",
                Description = "President Lyndon B. Johnson signs the Civil Rights Act of 1964, mandating equal opportunity employment, and complete desegregation of schools and other public facilities. It also outlaws unequal voter registration requirements. Although it would take years for these changes to take effect in communities around the country, the law is a monumental victory for the civil rights movement.",
                Emoticon = Utils.Emoticon.Neutral,
                IsDeleted = false,
                CreatedOn = new DateTime(2015, 5, 24, 15, 32, 20),
                CreatedBy = manager.Id,
                Type = Utils.PostType.Career,
                Manager = manager,
                Employee = employee,
                Category = conversationCategory
            };

            var comment6 = new Comment
            {
                Id = Guid.NewGuid(),
                Content = "President Lyndon B. Johnson signs the Civil Rights Act of 1964, mandating equal opportunity employment, and complete desegregation of schools and other public facilities. It also outlaws unequal voter registration requirements.",
                IsDeleted = false,
                CreatedOn = new DateTime(2015, 6, 1, 11, 30, 20),
                CreatedBy = manager.Id,
            };
            conversation5.Comments.Add(comment6);

            var tag4 = context.Tags.SingleOrDefault(t => t.Name == "personal");
            if (tag4 == null) tag4 = new Tag { Name = "personal" };
            conversation5.Tags.Add(tag4);

            conversation5 = context.Posts.Add(conversation5);
            context.SaveChanges();
        }

        private void AddConversation6(ApplicationUserManager userManager, ApplicationDbContext context)
        {
            var conversationCategory = context.Categories.SingleOrDefault(c => c.Name == "Conversation");
            if (conversationCategory == null) return;

            const string managerName = "Sriram.AswathNarayan@in.bosch.com";
            var manager = userManager.FindByName(managerName);

            const string employeeName = "Anil.Kadakol@in.bosch.com";
            var employee = userManager.FindByName(employeeName);

            // conversation 6
            var conversation6 = new Post
            {
                Id = Guid.NewGuid(),
                Title = "Conversation Title 6",
                Description = "President Lyndon B. Johnson signs the Civil Rights Act of 1964, mandating equal opportunity employment, and complete desegregation of schools and other public facilities. It also outlaws unequal voter registration requirements. Although it would take years for these changes to take effect in communities around the country, the law is a monumental victory for the civil rights movement.",
                Emoticon = Utils.Emoticon.Frown,
                IsDeleted = false,
                CreatedOn = new DateTime(2015, 6, 24, 16, 32, 20),
                CreatedBy = manager.Id,
                Type = Utils.PostType.Goals,
                Manager = manager,
                Employee = employee,
                Category = conversationCategory
            };

            var comment6 = new Comment
            {
                Id = Guid.NewGuid(),
                Content = "President Lyndon B. Johnson signs the Civil Rights Act of 1964, mandating equal opportunity employment, and complete desegregation of schools and other public facilities. It also outlaws unequal voter registration requirements.",
                IsDeleted = false,
                CreatedOn = new DateTime(2015, 6, 30, 11, 30, 20),
                CreatedBy = manager.Id,
            };
            conversation6.Comments.Add(comment6);

            var tag4 = context.Tags.SingleOrDefault(t => t.Name == "personal");
            if (tag4 == null) tag4 = new Tag { Name = "personal" };
            conversation6.Tags.Add(tag4);

            conversation6 = context.Posts.Add(conversation6);
            context.SaveChanges();
        }

        private void AddFeedback1(ApplicationUserManager userManager, ApplicationDbContext context)
        {
            var feedbackCategory = context.Categories.SingleOrDefault(c => c.Name == "Feedback");
            if (feedbackCategory == null) return;

            const string managerName = "Anil.Kadakol@in.bosch.com";
            var manager = userManager.FindByName(managerName);

            const string employeeName = "Benno.Oommen@in.bosch.com";
            var employee = userManager.FindByName(employeeName);

            // feedback 4
            var feedback4 = new Post
            {
                Id = Guid.NewGuid(),
                Title = "Feedback Title 1",
                Description = "When you meet with someone who is angry, you can use the tools of effective listening to help defuse this anger. Nevertheless, when anger is directed at you, it is much more difficult to respond definitively, because your own emotions are usually involved.",
                Emoticon = Utils.Emoticon.Smile,
                IsDeleted = false,
                CreatedOn = new DateTime(2015, 5, 24, 15, 32, 20),
                CreatedBy = manager.Id,
                IsPrivate = false,
                Manager = manager,
                Employee = employee,
                Category = feedbackCategory
            };

            var comment5 = new Comment
            {
                Id = Guid.NewGuid(),
                Content = "President Lyndon B. Johnson signs the Civil Rights Act of 1964, mandating equal opportunity employment, and complete desegregation of schools and other public facilities. It also outlaws unequal voter registration requirements.",
                IsDeleted = false,
                CreatedOn = new DateTime(2015, 6, 1, 11, 30, 20),
                CreatedBy = manager.Id,
            };
            feedback4.Comments.Add(comment5);

            var tag4 = context.Tags.SingleOrDefault(t => t.Name == "personal");
            if (tag4 == null) tag4 = new Tag { Name = "personal" };
            feedback4.Tags.Add(tag4);

            feedback4 = context.Posts.Add(feedback4);
            context.SaveChanges();
        }

        private void AddOrganization(ApplicationUserManager userManager, ApplicationDbContext context)
        {
            var buHead = userManager.FindByName("Vijay.Ratnaparkhe@in.bosch.com");
            var sectionHead = userManager.FindByName("Sriram.AswathNarayan@in.bosch.com");
            var departmentHead = userManager.FindByName("Rincy.Joseph@in.bosch.com");
            var groupManager = userManager.FindByName("Anil.Kadakol@in.bosch.com");
            var teamMember = userManager.FindByName("Benno.Oommen@in.bosch.com");

            var type1 = new OrgType { Name = "Global Entity", IsDeleted = false, CreatedOn = DateTime.UtcNow };
            var type2 = new OrgType { Name = "Region", IsDeleted = false, CreatedOn = DateTime.UtcNow };
            var type3 = new OrgType { Name = "Country", IsDeleted = false, CreatedOn = DateTime.UtcNow };
            var type4 = new OrgType { Name = "Legal Entity", IsDeleted = false, CreatedOn = DateTime.UtcNow };
            var type5 = new OrgType { Name = "Division", IsDeleted = false, CreatedOn = DateTime.UtcNow };
            var type6 = new OrgType { Name = "Department", IsDeleted = false, CreatedOn = DateTime.UtcNow };
            var type7 = new OrgType { Name = "Group", IsDeleted = false, CreatedOn = DateTime.UtcNow };

            context.OrgTypes.Add(type1); context.OrgTypes.Add(type2); context.OrgTypes.Add(type3); context.OrgTypes.Add(type4);
            context.OrgTypes.Add(type5); context.OrgTypes.Add(type6); context.OrgTypes.Add(type7);
            context.SaveChanges();

            var id1 = Guid.NewGuid();
            var org1 = new Org { Id = id1, Name = "RB", Description = "Robert Bosch", Type = type1, Path = string.Empty + id1, IsDeleted = false, CreatedOn = DateTime.UtcNow };

            var id2 = Guid.NewGuid();
            var org2 = new Org { Id = id2, Name = "APAC", Description = "Asian Pacific region", Type = type2, ParentId = org1.Id, Path = org1.Path + "/" + id2, IsDeleted = false, CreatedOn = DateTime.UtcNow };

            var id3 = Guid.NewGuid();
            var org3 = new Org { Id = id3, Name = "India", Description = "India", Type = type3, ParentId = org2.Id, Path = org2.Path + "/" + id3, IsDeleted = false, CreatedOn = DateTime.UtcNow };

            var id4 = Guid.NewGuid();
            var org4 = new Org { Id = id4, Name = "RBEI/P", Description = "RBEI/P", Manager = buHead, Type = type4, ParentId = org3.Id, Path = org3.Path + "/" + id4, IsDeleted = false, CreatedOn = DateTime.UtcNow };

            var id5 = Guid.NewGuid();
            var org5 = new Org { Id = id5, Name = "CI/ISY-IN", Description = "CI/ISY-IN", Manager = sectionHead, Type = type5, ParentId = org4.Id, Path = org4.Path + "/" + id5, IsDeleted = false, CreatedOn = DateTime.UtcNow };

            var id6 = Guid.NewGuid();
            var org6 = new Org { Id = id6, Name = "CI/BPR-IN", Description = "CI/BPR-IN", Manager = departmentHead, Type = type6, ParentId = org5.Id, Path = org5.Path + "/" + id6, IsDeleted = false, CreatedOn = DateTime.UtcNow };

            var id7 = Guid.NewGuid();
            var org7 = new Org { Id = id7, Name = "CI/BPR-IN", Description = "CI/BPR-IN", Manager = groupManager, Type = type7, ParentId = org6.Id, Path = org6.Path + "/" + id7, IsDeleted = false, CreatedOn = DateTime.UtcNow };

            context.Orgs.Add(org1); context.Orgs.Add(org2); context.Orgs.Add(org3);
            context.Orgs.Add(org4); context.Orgs.Add(org5); context.Orgs.Add(org6); context.Orgs.Add(org7);

            // Add User to Org
            org4.Employees.Add(buHead);
            org5.Employees.Add(sectionHead);
            org6.Employees.Add(departmentHead);
            org7.Employees.Add(groupManager);
            org7.Employees.Add(teamMember);

            context.SaveChanges();
        }

        private void AddBUHead(ApplicationDbContext context, ApplicationRoleManager roleManager, ApplicationUserManager userManager)
        {
            const string name = "Vijay.Ratnaparkhe@in.bosch.com";
            const string password = "ctm@2015";
            const string roleName = "BU Head";

            // Create Role if it does not exist
            var role = roleManager.FindByName(roleName);
            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            // Create User if it does not exist
            var user = userManager.FindByName(name);
            if (user == null)
            {
                user = new ApplicationUser
                {
                    UserName = name,
                    Email = name,
                    DisplayName = "Vijay Ratnaparkhe (RBEI/P)",
                    Avatar = string.Format("https://rb-owa.apac.bosch.com/ews/exchange.asmx/s/GetUserPhoto?email={0}&size=HR240x240", name),
                    NTID = "vij9kor"
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role if not already added
            var rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }

            // Create Permissions if they do not exist
            var permissions = new List<Permission> {
                // Conversation
                new Permission { Code = Rights.View + Modules.Conversation, Description = "View Conversation", Name = "View Conversation" },
                new Permission { Code = Rights.Create + Modules.Conversation, Description = "Create Conversation", Name = "Create Conversation" },
                new Permission { Code = Rights.Modify + Modules.Conversation, Description = "Modify Conversation", Name = "Modify Conversation" },
                new Permission { Code = Rights.Delete + Modules.Conversation, Description = "Delete Conversation", Name = "Delete Conversation" },
            };

            foreach (var permission in permissions)
            {
                var permissionInDb = context.Permissions.FirstOrDefault(x => x.Code == permission.Code);
                if (permissionInDb == null)
                {
                    permissionInDb = context.Permissions.Add(permission);
                }
                // Add permissions to Role Admin if not already added
                role.Permissions.Add(permissionInDb);
            }
            context.SaveChanges();
        }

        private void AddSectionHead(ApplicationDbContext context, ApplicationRoleManager roleManager, ApplicationUserManager userManager)
        {
            const string name = "Sriram.AswathNarayan@in.bosch.com";
            const string password = "ctm@2015";
            const string roleName = "Section Head";

            // Create Role if it does not exist
            var role = roleManager.FindByName(roleName);
            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            // manager
            const string managerName = "Vijay.Ratnaparkhe@in.bosch.com";
            var manager = userManager.FindByName(managerName);

            // Create User if it does not exist
            var user = userManager.FindByName(name);
            if (user == null)
            {
                user = new ApplicationUser
                {
                    UserName = name,
                    Email = name,
                    DisplayName = "Sriram N A (CI/ISY-IN)",
                    Avatar = string.Format("https://rb-owa.apac.bosch.com/ews/exchange.asmx/s/GetUserPhoto?email={0}&size=HR240x240", name),
                    Manager = manager,
                    NTID = "sna8ban"
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user to Role if not already added
            var rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }

            // Create Permissions if they do not exist
            var permissions = new List<Permission> {
                // Conversation
                new Permission { Code = Rights.View + Modules.Conversation, Description = "View Conversation", Name = "View Conversation" },
                new Permission { Code = Rights.Create + Modules.Conversation, Description = "Create Conversation", Name = "Create Conversation" },
                new Permission { Code = Rights.Modify + Modules.Conversation, Description = "Modify Conversation", Name = "Modify Conversation" },
                new Permission { Code = Rights.Delete + Modules.Conversation, Description = "Delete Conversation", Name = "Delete Conversation" },
            };

            foreach (var permission in permissions)
            {
                var permissionInDb = context.Permissions.FirstOrDefault(x => x.Code == permission.Code);
                if (permissionInDb == null)
                {
                    permissionInDb = context.Permissions.Add(permission);
                }
                // Add permissions to Role Admin if not already added
                role.Permissions.Add(permissionInDb);
            }
            context.SaveChanges();
        }

        private void AddDepartmentHead(ApplicationDbContext context, ApplicationRoleManager roleManager, ApplicationUserManager userManager)
        {
            const string name = "Rincy.Joseph@in.bosch.com";
            const string password = "ctm@2015";
            const string roleName = "Department Head";

            // Create Role if it does not exist
            var role = roleManager.FindByName(roleName);
            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            // manager
            const string managerName = "Sriram.AswathNarayan@in.bosch.com";
            var manager = userManager.FindByName(managerName);

            // Create User if it does not exist
            var user = userManager.FindByName(name);
            if (user == null)
            {
                user = new ApplicationUser
                {
                    UserName = name,
                    Email = name,
                    DisplayName = "Rincy Joseph (CI/BPR-IN)",
                    Avatar = string.Format("https://rb-owa.apac.bosch.com/ews/exchange.asmx/s/GetUserPhoto?email={0}&size=HR240x240", name),
                    Manager = manager,
                    NTID = "rij2kor"
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user to Role if not already added
            var rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }

            // Create Permissions if they do not exist
            var permissions = new List<Permission> {
                // Conversation
                new Permission { Code = Rights.View + Modules.Conversation, Description = "View Conversation", Name = "View Conversation" },
                new Permission { Code = Rights.Create + Modules.Conversation, Description = "Create Conversation", Name = "Create Conversation" },
                new Permission { Code = Rights.Modify + Modules.Conversation, Description = "Modify Conversation", Name = "Modify Conversation" },
                new Permission { Code = Rights.Delete + Modules.Conversation, Description = "Delete Conversation", Name = "Delete Conversation" },
            };

            foreach (var permission in permissions)
            {
                var permissionInDb = context.Permissions.FirstOrDefault(x => x.Code == permission.Code);
                if (permissionInDb == null)
                {
                    permissionInDb = context.Permissions.Add(permission);
                }
                // Add permissions to Role Admin if not already added
                role.Permissions.Add(permissionInDb);
            }
            context.SaveChanges();
        }

        private void AddGroupManager(ApplicationDbContext context, ApplicationRoleManager roleManager, ApplicationUserManager userManager)
        {
            const string name = "Anil.Kadakol@in.bosch.com";
            const string password = "ctm@2015";
            const string roleName = "Group Manager";

            // Create Role if it does not exist
            var role = roleManager.FindByName(roleName);
            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            // manager
            const string managerName = "Rincy.Joseph@in.bosch.com";
            var manager = userManager.FindByName(managerName);

            // Create User if it does not exist
            var user = userManager.FindByName(name);
            if (user == null)
            {
                user = new ApplicationUser
                {
                    UserName = name,
                    Email = name,
                    DisplayName = "Anil Kadakol (CI/BPR-IN)",
                    Avatar = string.Format("https://rb-owa.apac.bosch.com/ews/exchange.asmx/s/GetUserPhoto?email={0}&size=HR240x240", name),
                    Manager = manager,
                    NTID = "ank2kor"
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role if not already added
            var rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }

            // Create Permissions if they do not exist
            var permissions = new List<Permission> {
                // Conversation
                new Permission { Code = Rights.View + Modules.Conversation, Description = "View Conversation", Name = "View Conversation" },
                new Permission { Code = Rights.Create + Modules.Conversation, Description = "Create Conversation", Name = "Create Conversation" },
                new Permission { Code = Rights.Modify + Modules.Conversation, Description = "Modify Conversation", Name = "Modify Conversation" },
                new Permission { Code = Rights.Delete + Modules.Conversation, Description = "Delete Conversation", Name = "Delete Conversation" },
            };

            foreach (var permission in permissions)
            {
                var permissionInDb = context.Permissions.FirstOrDefault(x => x.Code == permission.Code);
                if (permissionInDb == null)
                {
                    permissionInDb = context.Permissions.Add(permission);
                }
                // Add permissions to Role Admin if not already added
                role.Permissions.Add(permissionInDb);
            }
            context.SaveChanges();
        }

        private void AddTeamMember(ApplicationDbContext context, ApplicationRoleManager roleManager, ApplicationUserManager userManager)
        {
            const string name = "Benno.Oommen@in.bosch.com";
            const string password = "ctm@2015";
            const string roleName = "Team Member";

            // Create Role User if it does not exist
            var role = roleManager.FindByName(roleName);
            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            // manager
            const string managerName = "Anil.Kadakol@in.bosch.com";
            var manager = userManager.FindByName(managerName);

            // Create User if it does not exist
            var user = userManager.FindByName(name);
            if (user == null)
            {
                user = new ApplicationUser
                {
                    UserName = name,
                    Email = name,
                    DisplayName = "Benno Oommen (CI/BPR-IN)",
                    Avatar = string.Format("https://rb-owa.apac.bosch.com/ews/exchange.asmx/s/GetUserPhoto?email={0}&size=HR240x240", name),
                    Manager = manager,
                    NTID = "boe1kor"
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user to Role User if not already added
            var rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }

            // Create Permissions if they do not exist
            var permissions = new List<Permission> {
                // Conversation
                new Permission { Code = Rights.View + Modules.Conversation, Description = "View Conversation", Name = "View Conversation" },
                new Permission { Code = Rights.Create + Modules.Conversation, Description = "Create Conversation", Name = "Create Conversation" },
                new Permission { Code = Rights.Modify + Modules.Conversation, Description = "Modify Conversation", Name = "Modify Conversation" },
                new Permission { Code = Rights.Delete + Modules.Conversation, Description = "Delete Conversation", Name = "Delete Conversation" },
            };

            foreach (var permission in permissions)
            {
                var permissionInDb = context.Permissions.FirstOrDefault(x => x.Code == permission.Code);
                if (permissionInDb == null)
                {
                    permissionInDb = context.Permissions.Add(permission);
                }
                // Add permissions to Role Admin if not already added
                role.Permissions.Add(permissionInDb);
            }
            context.SaveChanges();
        }

        private void AddHR(ApplicationDbContext context, ApplicationRoleManager roleManager, ApplicationUserManager userManager)
        {
            const string name = "quynh.nguyennhat@vn.bosch.com";
            const string password = "ctm@2015";
            const string roleName = "HR";

            // Create Role if it does not exist
            var role = roleManager.FindByName(roleName);
            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            // Create User if it does not exist
            var user = userManager.FindByName(name);
            if (user == null)
            {
                user = new ApplicationUser
                {
                    UserName = name,
                    Email = name,
                    DisplayName = "Nguyen Nhat Quynh (RBVH/ETI1)",
                    Avatar = string.Format("https://rb-owa.apac.bosch.com/ews/exchange.asmx/s/GetUserPhoto?email={0}&size=HR240x240", name),
                    NTID = "qun8hc"
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role if not already added
            var rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }

            // Create Permissions if they do not exist
            var permissions = new List<Permission> {
                // Conversation
                new Permission { Code = Rights.View + Modules.Conversation, Description = "View Conversation", Name = "View Conversation" },
                new Permission { Code = Rights.Create + Modules.Conversation, Description = "Create Conversation", Name = "Create Conversation" },
                new Permission { Code = Rights.Modify + Modules.Conversation, Description = "Modify Conversation", Name = "Modify Conversation" },
                new Permission { Code = Rights.Delete + Modules.Conversation, Description = "Delete Conversation", Name = "Delete Conversation" },
            };

            foreach (var permission in permissions)
            {
                var permissionInDb = context.Permissions.FirstOrDefault(x => x.Code == permission.Code);
                if (permissionInDb == null)
                {
                    permissionInDb = context.Permissions.Add(permission);
                }
                // Add permissions to Role Admin if not already added
                role.Permissions.Add(permissionInDb);
            }
            context.SaveChanges();
        }

        private void AddAdministrator(ApplicationDbContext context, ApplicationRoleManager roleManager, ApplicationUserManager userManager)
        {
            const string name = "tin.nguyenhoang@vn.bosch.com";
            const string password = "ctm@2015";
            const string roleName = "Administrator";

            // Create Role if it does not exist
            var role = roleManager.FindByName(roleName);
            if (role == null)
            {
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            // Create User if it does not exist
            var user = userManager.FindByName(name);
            if (user == null)
            {
                user = new ApplicationUser
                {
                    UserName = name,
                    Email = name,
                    DisplayName = "Nguyen Hoang Tin (RBVH/ETI1)",
                    Avatar = string.Format("https://rb-owa.apac.bosch.com/ews/exchange.asmx/s/GetUserPhoto?email={0}&size=HR240x240", name),
                    NTID = "tig1hc"
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role if not already added
            var rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }

            // Create Permissions if they do not exist
            var permissions = new List<Permission> {
                // Role
                new Permission { Code = Rights.View + Modules.Role, Description = "View Role", Name = "View Role" },
                new Permission { Code = Rights.Create + Modules.Role, Description = "Create Role", Name = "Create Role" },
                new Permission { Code = Rights.Modify + Modules.Role, Description = "Modify Role", Name = "Modify Role" },
                new Permission { Code = Rights.Delete + Modules.Role, Description = "Delete Role", Name = "Delete Role" },
                // Conversation
                new Permission { Code = Rights.View + Modules.Conversation, Description = "View Conversation", Name = "View Conversation" },
                new Permission { Code = Rights.Create + Modules.Conversation, Description = "Create Conversation", Name = "Create Conversation" },
                new Permission { Code = Rights.Modify + Modules.Conversation, Description = "Modify Conversation", Name = "Modify Conversation" },
                new Permission { Code = Rights.Delete + Modules.Conversation, Description = "Delete Conversation", Name = "Delete Conversation" },
            };

            foreach (var permission in permissions)
            {
                var permissionInDb = context.Permissions.FirstOrDefault(x => x.Code == permission.Code);
                if (permissionInDb == null)
                {
                    permissionInDb = context.Permissions.Add(permission);
                }
                // Add permissions to Role Admin if not already added
                role.Permissions.Add(permissionInDb);
            }
            context.SaveChanges();
        }
    }
}
